<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
	<head>
		<jsp:include page="../inc.jsp"></jsp:include>
		<script type="text/javascript" src="../My97DatePicker/WdatePicker.js"></script>
		<script type="text/javascript" charset="utf-8">
		var lastIndex,id = '${id}';
		var IsInConfirm = '${IsInConfirm}';
		var IsOutConfirm = '${IsOutConfirm}';
		var options={};
		var json; 
 		$(function() {
			var agencyName = $('#receiveForm').find('input[name=agencyName]');
			agencyName.val('${agencyName}');

			var orname = $('#receiveForm').find('input[name=orname]');
			orname.val('${orname}');
			
			var num = $('#receiveForm').find('input[name=num]');
			num.val('${num}');
			
			var date = $('#receiveForm').find('input[name=date]');
			date.val('${date}');
			
			var serial = $('#receiveForm').find('input[name=serial]');
			serial.val('${serial}');
			
			var uid = $('#receiveForm').find('input[name=uid]');
			var uidCombobox = uid.combobox({
				url : '${pageContext.request.contextPath}/qxgl/userAction!noAuth_userInReperCombobox.html',
				valueField : 'id',
				textField : 'username',
				multiple : false,
				editable : false,
				panelHeight : 'auto'
			});

			
            
            datagrid = $('#dg').datagrid({
				url : '${pageContext.request.contextPath}/kcgl/goodsDeliverItemAction!datagrid.html',
				title : '收货明细列表',
				iconCls : 'icon-save',
				queryParams:{dlid:id},
				pagination : true,
				pagePosition : 'bottom',
				pageSize : 10,
				pageList : [ 10, 20, 30, 40 ],
				fit : true,
				fitColumns : false,
				nowrap : false,
				border : false,
				idField : 'id',
				checkOnSelect : false,
				selectOnCheck : false,
				singleSelect : true,
				onDblClickCell: function (rowIndex, field, value) {
					var rows = $('#dg').datagrid("getRows");
					if(rows[rowIndex].isConfirm!=true){
						$(dg).datagrid('beginEdit', rowIndex);
	                    var ed = $(this).datagrid('getEditor', {index:rowIndex,field:field});
	               		$(ed.target).focus();
	                    $(ed.target).select();
	                }
    			},
    			toolbar:[{
					text: '确定',
					iconCls:'icon-edit',
					handler:function(){
						$('#dg').datagrid('acceptChanges');
						sumNum();
					}
				},'-',{
					text: '生成进货单',
					iconCls:'icon-add',
					handler:function(){
						var orders = new Array();
						var rows = $('#dg').datagrid("getChecked");
						var deliverlistItems = "";
						for(var d = 0;d < rows.length;d++){
							deliverlistItems += rows[d].id+",";
							deliverlistItems += rows[d].dealerGoodsId+",";
						}
						deliverlistItems = deliverlistItems.substr(0, deliverlistItems.length-1);
						for(var i = 0;i<json.length;i++){
							var order = new Object();
							order.rid = json[i].id;
							order.orderItems = new Array();
							var orderItem = "";
							for(var j = 0;j < rows.length;j++){
								orderItem += rows[j].dealerGoodsId+",";
								orderItem += rows[j].price+",";
								 s = "rows["+j+"].num"+i;
								orderItem += eval(s)+",";
								j = j+2;
							}
							order.orderItems = orderItem.substr(0, orderItem.length-1);
							orders[i] = order;
						}	
						var orderString = JSON.stringify(orders);
						alert(orderString);
						createOrders(orderString,deliverlistItems);
					}
				}],
			});
    		getRepertories();
		});
		
			//获取所有仓库并增加列到datagrid
			function getRepertories(){
				$.post("${pageContext.request.contextPath}/rkgl/repertoryAction!noAuth_comboboxAll.html",
                	function(data){
                		if(data.length>0){
	               		json = JSON.parse(data);
	               		var ss = "";
						for(var i = 0;i<json.length;i++){
							ss += ",{title:'"+json[i].name+"',field:'num"+i+"',width:80,editor:'numberbox',}";
						}
					fetchData(ss);
					}
				});
			}
            
            //拼装datagrid
			function fetchData(ss) { 
			    var s = "[[{title:'编号',field:'id',	width:80,sortable:true,checkbox:true},"
						+"{title:'商品编号',field:'gid',width:80,hidden:true,},"
						+"{title:'商品条码',field:'gbarcode',width:80,},"
						+"{title:'商品名称',field:'gname',width:80,},"
						+"{title:'品牌',field:'gbrand',width:80,},"
						+"{title:'款号',field:'gtype',width:80,},"
						+"{title:'颜色',field:'gcolor',width:40,},"
						+"{title:'尺码',field:'gsize',width:40,},"
						+"{title:'单价',field:'price',width:35,},"
						+"{title:'匹配商品编号',field:'dealerGoodsId',width:80,hidden:true,},"
						+"{title:'匹配商品名称',field:'dealerGoodsName',width:80,},"
						+"{title:'是否确认',field:'isConfirm',width:55,formatter:function(value, row, index){if(value==true){return '已确认'}else{return '未确认';}}},"
						+"{title:'收货数量',field:'num',width:55,sortable:true,}";
				s = s + ss;
			    s = s + "]]"; 
			    options={}; 
			    options.columns = eval(s);
			    options.columns[0].splice(11,0,
			        { 
						title:'匹配',field:'dealerGoods',width:35, 
			            formatter:function(value, row, index){
			           		var rows = $('#dg').datagrid("getRows");
							if(rows[index].isConfirm!=true){
			            		return "<img onclick='javascript:matchGoods("+row.gid+","+index+");' style='cursor:pointer;' src='${pageContext.request.contextPath}/style/images/extjs_icons/table_edit.png'/>";
			            	}else{
			            		return "";
			            	}
			            } 
			        }    
			    );
			    $('#dg').datagrid(options);
			    $('#dg').datagrid('reload');
			} 
			
			//生成进货单
			function createOrders(orderString,deliverlistItems){
				parent.sy.messagerConfirm('请确认', '您要将选中条目生成为进货单？', function(r) {
					if (r) {
						$.ajax({
							url : '${pageContext.request.contextPath}/rkgl/orderAction!deliverAddOrder.html',
							data : {items:orderString,deliverlistItems:deliverlistItems},
							dataType : 'json',
							success : function(d) {
								datagrid.datagrid('load');
								datagrid.datagrid('unselectAll');
								parent.sy.messagerShow({
									title : '提示',
									msg : d.msg
								});
							}
						});
					}
				});
			}
			
			//匹配商品方法
			function matchGoods(gid,index){
				var p = parent.sy.dialog({
					title : '选择匹配商品',
					href : '${pageContext.request.contextPath}/kcgl/goodsDeliverItemAction!noAuth_goodsReceiveMatch.html',
					width : 700,
					height : 300,
					buttons : [ {
						text : '确定',
						handler : function() {
							p.dialog('close');
						}
					}],
					onLoad : function() {					
						var goodsMatch = p.find('table[id=goodsMacth]');
						var goodsMatchDatagrid = goodsMatch.datagrid({
							url : '${pageContext.request.contextPath}/spgl/goodsAction!goodsMatchDatagrid.html',
							title : '商品列表',
							iconCls : 'icon-save',
							queryParams:{id:gid},
							pagination : true,
							checkOnSelect : false,
							selectOnCheck : false,
							singleSelect : true,
							onDblClickRow: function (rowIndex, rowData) {
								var row = $("#dg tr:eq("+index+")");
								row.dealerGoodsId=rowData.id;
								row.dealerGoodsName=rowData.name;
								$('#dg').datagrid('updateRow',{index: rowIndex, row:row});
								$('#dg').datagrid('refreshRow', rowIndex);
								p.dialog('close');
			    			},
							frozenColumns : [ [ {
								title : '编号',
								field : 'id',
								width : 80,
								hidden : true,
							}, {
								title : '商品条码',
								field : 'barcode',
								width : 125,
								sortable : true,
								
							},  {
								title : '款号',
								field : 'type',
								width : 90,
								sortable : true,
							},] ],
							columns : [ [  {
								title : '商品类别ID',
								field : 'gtid',
								width : 100,
								hidden : true,
								sortable : true,
							},  {
								title : '商品名称',
								field : 'name',
								width : 120,
								sortable : true,
							}, {
								title : '品牌ID',
								field : 'bid',
								width : 100,
								hidden : true,
								sortable : true,
							},  {
								title : '品牌',
								field : 'bname',
								width : 80,
							}, {
								title : '商品类别',
								field : 'gtname',
								width : 90,
								align:'center',
							}, {
								title : '颜色',
								field : 'color',
								width : 60,
								align:'center',
								sortable : true,
							}, {
								title : '尺码',
								field : 'size',
								width : 60,
								align:'center',
								sortable : true,
							},{
								title : '单位',
								field : 'unit',
								width : 50,
								align:'center'
							}, {
								title : '吊牌价',
								field : 'showPrice',
								width : 60,
								align:'center',
								sortable : true,
							}, {
								title : '折扣ID',
								field : 'did',
								width : 80,
								hidden : true,
								sortable : true,
							}, {
								title : '进价',
								field : 'buyPrice',
								width : 60,
								align:'center',
								sortable : true,
							},{
								title : '备注',
								field : 'remark',
								width : 150,
								hidden : true
							} ] ],
							onRowContextMenu : function(e, rowIndex, rowData) {
								e.preventDefault();
								$(this).datagrid('unselectAll');
								$(this).datagrid('selectRow', rowIndex);
								$('#menu').menu('show', {
									left : e.pageX,
									top : e.pageY
								});
							}
						});
					}
				});
			}

	</script>
	</head>
	<body class="easyui-layout" data-options="fit:true">
		<c:if test="${IsInConfirm==false&&IsOutConfirm==true}">
		<div data-options="region:'north',border:false" style="height: 100px; overflow: hidden;">
			<form id="receiveForm" style="height:110px;">
				<input name="id" type="hidden"/>
				<input id="items" name="items" type="hidden" />
				<input id="iids" name="iids" type="hidden" />
				
				<input id="nums" name="nums" type="hidden" />
				<input id="ids" name="ids" type="hidden" />
				<table class="tableForm datagrid-toolbar" style="width: 100%; height: 100%;">
					<tr>
						<th style="width: 80px;">发货日期</th>
						<td style="width: 200px;"><input name="date" style="width: 200px;"  readonly="readonly"/></td>
						<th style="width: 80px;">单&nbsp;&nbsp;&nbsp;&nbsp;号</th>
						<td style="width: 200px;"><input name="serial" style="width: 200px;" readonly="readonly" id="serial"/></td>
						<th style="width: 80px;">操&nbsp;作&nbsp;员</th>
						<td style="width: 200px;"><input name="uid" style="width: 200px;" data-options="required:'true',missingMessage:'请选择操作员'"/></td>
					</tr>
					<tr>
						<th>发货代理商 </th>
						<td><input name="agencyName" style="width: 200px;" readonly="readonly"  /></td>
						<th>发出仓库</th>
						<td><input name="orname" readonly="readonly"  style="width: 200px;" /></td>
						<th>发货总数</th>
						<td><input name="num" id="num" style="width: 200px;" type="text" readonly="readonly"/></td>
					</tr>
					<tr>
						<th>备&nbsp;&nbsp;&nbsp;&nbsp;注</th>
						<td colspan="5"><textarea name="remark" style="height:30px;width: 95%;"></textarea></td>
					</tr>
				</table>
			</form>
			</div>
		</c:if>
		<c:if test="${IsInConfirm==true&&IsOutConfirm==true}">
		<div data-options="region:'north',border:false" style="height: 100px; overflow: hidden;">
			<form id="receiveForm" style="height:110px;">
				<input name="id" type="hidden"/>
				<input id="items" name="items" type="hidden" />
				<input id="iids" name="iids" type="hidden" />
				
				<input id="nums" name="nums" type="hidden" />
				<input id="ids" name="ids" type="hidden" />
				<table class="tableForm datagrid-toolbar" style="width: 100%; height: 100%;">
					<tr>
						<th style="width: 80px;">日&nbsp;&nbsp;&nbsp;&nbsp;期</th>
						<td style="width: 200px;"><input name="date"  readonly="readonly" style="width: 200px;" /></td>
						<th style="width: 80px;">单&nbsp;&nbsp;&nbsp;&nbsp;号</th>
						<td style="width: 200px;"><input name="serial" style="width: 200px;" readonly="readonly" id="serial"/></td>
						<th style="width: 80px;">操&nbsp;作&nbsp;员</th>
						<td style="width: 200px;"><input name="uid" style="width: 200px;" data-options="required:'true',missingMessage:'请选择操作员'"/></td>
					</tr>
					<tr>
						<th>发货代理商 </th>
						<td><input name="agencyName" style="width: 200px;" readonly="readonly"/></td>
						<th>发出仓库</th>
						<td><input name="orname" readonly="readonly"  style="width: 200px;" /></td>
						<th>发货总数</th>
						<td><input name="num" id="num" style="width: 200px;" type="text" readonly="readonly"/></td>
					</tr>
					<tr>
						<th>备&nbsp;&nbsp;&nbsp;&nbsp;注</th>
						<td colspan="5"><textarea name="remark" style="height:30px;width: 95%;"></textarea></td>
					</tr>
				</table>
			</form>
			</div>
		</c:if>
		<div data-options="region:'center',border:false" style="overflow: hidden;">
			<table id="dg" class="easyui-datagrid" style="height:auto"  data-options="iconCls: 'icon-edit',singleSelect: true">  
		    </table>
		</div>
		<!-- 匹配商品弹窗 -->
		<div id="mydialog" style="display:none;padding:5px;width:400px;height:auto;min-height:150px;max-height:200px;overflow-y:scrooll;" title="亲！该条码对应了多件商品，请双击您要销售的商品。">		 		
		<table id="mytable" class="easyui-datagrid" style="height:auto"  data-options="iconCls: 'icon-edit',singleSelect: true"> 
		    </table>
		</div> 
	</body>
</html>